function [IMFiles , realFold , savedfeaturesfolder , IMSDESCNAMES,IMS ] =writeImageFeatures( seqname,matchpre,limit,override )

% a lot of the things here are taken from peter kovesi
%http://www.csse.uwa.edu.au/~pk/research/matlabfns/Robust/example/index.html

matchtech=matchpre; % 1 for haris with correlation, 2 for sift
thresh = 60;   % Harris corner threshold




maxfunds=15;
writefiles=0;
%data paths

[cdirname,IMFiles,realFold] = findCorrectDataDirectory(seqname);
numimages=size(IMFiles,1);

if(limit>0 && numimages>limit)
    numimages=limit;
end



matchprefixname={'harris','sift'};
savedfeaturesfolder=[realFold  'matfiles' seqname];

if(exist(savedfeaturesfolder,'dir')~=0 && override==1)
 
    [status, message, messageid] =  rmdir(savedfeaturesfolder,'s');
end

if(exist(savedfeaturesfolder,'dir')==0)
    mkdir(savedfeaturesfolder);
end

IMSDESCNAMES=cell(1,numimages);
for i=1:numimages
    
    IMS{1,i}=imread(  [realFold  IMFiles(i,1).name ]);
    [~, name, ~] = fileparts( [realFold  '' IMFiles(i,1).name ]) ;
    if(matchtech==1 || matchtech==2 )
        IMSDESCNAMES{1,i}= [savedfeaturesfolder  '/' name matchprefixname{1,matchtech} '.mat' ];
    else
        error('match technique should be either 2 or 1');
    end
    
end

infofile=[savedfeaturesfolder  '/features_' seqname '_' matchprefixname{matchtech} '.txt'];

createstatfile=0;
if(exist(infofile,'file')==0)
    fidstat = fopen(infofile,'w');
    fprintf(fidstat,'image num , number of features \n');
    createstatfile=1;
end

for i=1:numimages
    tic
    clear cim1 r1 c1 cim2 r2 c2 m1 m2 F   inliers im1 im2 m1 m2 x1 x2 I1 I2 frames1 descriptors1 frames2 descriptors2 xin1 xin2;
    
    [~,~,vj1]=size(IMS{1,i});
    
    if(vj1>1 )
        im1 = 	rgb2gray(IMS{1,i});
    else
        im1 = 	(IMS{1,i});
    end
    
    if(matchtech==1)
        if(exist(IMSDESCNAMES{1,i},'file')==0)
            [cimfile, rfile,cfile] = harris(im1, 1, thresh, 3);
            save(IMSDESCNAMES{1,i},'cimfile','rfile','cfile');
            numfeatures=size( rfile,1);
            tf = toc;
            display(['done with frame ' num2str(i) ' and it took ' num2str(tf) ' seconds and found features are: ' num2str( numfeatures)]);
            figure('Visible','off'); show(im1,0), hold on, plot(cfile,rfile,'r+');
            saveas(gcf,[savedfeaturesfolder  '/' seqname '_features_im_'   '_' matchprefixname{matchtech} '_' num2str(i) '.jpg']);
            if(createstatfile==1)
                fprintf(fidstat,[ num2str(i) ' , '  num2str(numfeatures) ' \n ']);
            end
        end
    else
        I1 = double(im1) ;
        I1=I1/256;
        
        if(exist(IMSDESCNAMES{1,i},'file')==0)
            [frames,descriptors] = sift(I1, 'Verbosity', 0) ;
            save(IMSDESCNAMES{1,i},'frames','descriptors');
            numfeatures=size(descriptors,2);
            tf = toc;
            display(['done with frame ' num2str(i) ' and it took ' num2str(tf) ' seconds and found features are: ' num2str( numfeatures)]);
            figure('Visible','off');  show(im1,0), hold on, plot(frames(1,:),frames(2,:),'r+');
            saveas(gcf,[savedfeaturesfolder  '/' seqname '_features_im_'   '_' matchprefixname{matchtech} '_' num2str(i) '.jpg']);
            if(createstatfile==1)
                fprintf(fidstat,[ num2str(i) ' , '  num2str(numfeatures) ' \n ']);
            end
        end
        
        
    end
    
    
    
    
    
    
    
    
    
    
end

if(createstatfile==1)
    fclose(fidstat);
end

end
